package com.criticalritrpg;


import java.util.Date;
import java.util.GregorianCalendar;

import com.criticalritrpg.bd.BDService;
import com.criticalritrpg.fichas.TabWidgetFichaActivity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Paint.Join;
import android.os.Bundle;
import android.support.v4.util.LogWriter;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class VerAgendaActivity extends Activity{
	//EditText mText;
	//Button mAdd;
	ListView mList;
	BDService mHelper;
	SQLiteDatabase mDb;
	Cursor mCursor;
	Menu menu_agenda;
	SimpleCursorAdapter mAdapter;
	int ano,mes,dia,hora,min,id_jogo,sessaoSelecionada;
	String local;
	boolean D = true;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ver_agenda);
		//mText = (EditText)findViewById(R.id.name2);
		//mAdd = (Button)findViewById(R.id.add2);
		//mAdd.setOnClickListener(this);
		mList = (ListView)findViewById(R.id.list);
		//mList.setOnItemClickListener(this);
		mHelper = new BDService(this);
		menu_agenda = (Menu) findViewById(R.menu.menu_agenda);
		id_jogo = getIntent().getFlags()+1;
		Log.d("", ""+id_jogo);
	}
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//Open connections to the database
		mDb = mHelper.getWritableDatabase();
		
		String[] columns = new String[] {"_id",BDService.COL_DATE_SESSAO, BDService.COL_LOCAL_SESSAO,BDService.FK_JOGOID};
		mCursor = mDb.query(BDService.TABLE_NAME_AGENDA, columns, BDService.FK_JOGOID+" = "+id_jogo, null, null, null, null);
		//Refresh the list
		String[] headers = new String[] {BDService.COL_DATE_SESSAO, BDService.COL_LOCAL_SESSAO};
		mAdapter = new SimpleCursorAdapter(this, android.R.layout.two_line_list_item,
		mCursor, headers, new int[]{android.R.id.text1, android.R.id.text2});
		mList.setAdapter(mAdapter);
		mList.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				arg1.setSelected(true);
				Toast.makeText(getApplicationContext(),((SQLiteCursor)arg0.getAdapter().getItem(arg2)).getString(0), Toast.LENGTH_SHORT).show();
				sessaoSelecionada = ((SQLiteCursor)arg0.getAdapter().getItem(0)).getInt(0);
				
				
			}
		});
		
	}
	
	@Override
	public void onPause() {
		super.onPause();
		//Close all connections
		mDb.close();
		mCursor.close();
	}
	
	public void onClick(View v) {
		if(D){
			Log.d("click", "id view" + v.getId());
		}
		
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		//super.onActivityResult(requestCode, resultCode, data);
		ano = data.getExtras().getInt(AgendaActivity.ANO);
		mes = data.getExtras().getInt(AgendaActivity.MES);
		dia = data.getExtras().getInt(AgendaActivity.DIA);
		hora = data.getExtras().getInt(AgendaActivity.HORA);
		min = data.getExtras().getInt(AgendaActivity.MIN);
		
		local = data.getExtras().getString(AgendaActivity.LOCAL);
		
		GregorianCalendar g  = new GregorianCalendar(ano, mes, dia, hora, min);
		Date d = g.getTime();
		if(ano!=0){
			mHelper.insereAgendaData(d, id_jogo, local);
			
			Toast.makeText(getApplicationContext(),"dia: "+dia+"hora: "+hora+"min: "+min, Toast.LENGTH_SHORT).show();
			}
		//mText.setText(null);
		
		
		
	}
	
	 @Override
	    public boolean onCreateOptionsMenu(Menu menu) 
	    {
	        MenuInflater inflater = getMenuInflater();
	        inflater.inflate(R.menu.menu_agenda, menu);
	        return true;
	    }
	 
	 @Override
	    public boolean onOptionsItemSelected(MenuItem item) 
	    {
	        Intent serverIntent = null;
	        switch (item.getItemId()) 
	        {
	        case R.id.add_agenda:
	            // Launch the DeviceListActivity to see devices and do scan
	            serverIntent = new Intent(this, AgendaActivity.class);
	            startActivityForResult(serverIntent, 0);
	            return true;
	        case R.id.deletar_agenda:
	            // Launch the DeviceListActivity to see devices and do scan
	            mDb.delete(BDService.TABLE_NAME_AGENDA, "_id = "+ sessaoSelecionada, null);
	            mAdapter.notifyDataSetChanged();
	            
	            return true;
	         
	        case R.id.jogar_ficha:
	        	serverIntent = new Intent(this,TabWidgetFichaActivity.class);
	        	serverIntent.addFlags(-1);
	        	startActivity(serverIntent);
	        	
	        	return true;
	        
	        case R.id.mestrar:
	        	serverIntent = new Intent(this,TabWidgetFichaActivity.class);
	        	serverIntent.addFlags(id_jogo);
	        	startActivity(serverIntent);
	        	return true;
	        case R.id.add_jogadores:
	        	serverIntent = new Intent(this,AddJogadores.class);
	        	serverIntent.addFlags(id_jogo);
	        	startActivity(serverIntent);
	        	return true;
	        }
	        return false;
	    }

	

	 
	 
	 
	 
	 
	 
	 
	 
	 
	 
}
